package com.genius.workflow.util;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.genius.workflow.config.DefaultConfiguration;
import com.mysql.cj.xdevapi.JsonArray;

public class GeniusJsonUtil {
	/**
	 * 公共转换参数
	 * @param obj
	 * @param resource
	 * @return
	 */
	public static JSONObject convertResource(JSONObject obj,JSONObject resource){
		for(String key:resource.keySet()){
			obj.put(key, resource.getJSONObject(key).getString(obj.getString(key)));
		}
		return obj;
	}
	public static boolean JsonKeyContainsStr(JSONObject json,String str){
		boolean isExist = false;
		for(String key:json.keySet()){
			if(key.contains(str)){
				isExist = true;
				break;
			}
		}
		return isExist;
	}
	/**
	 * 适用于将一堆字符串转换成jsonarr
	 * 
	 * @param field
	 * @return 返回拼接成的的JSONArray
	 */
	public static JSONArray getJsonArrByField(String field){
		String [] fields = field.split(",");
		JSONArray arr = new JSONArray();
		for(int index=0;index<fields.length;index++){
			arr.add(fields[index]);
		}
		return arr;
	}
	
	public static JSONArray parseHighSearchJson(JSONObject json){
		JSONArray arr = new JSONArray();
		int rowLength = json.getIntValue("rowLength");
		for(int index=0;index<rowLength;index++){
			JSONObject searchJson = new JSONObject();
			String [] convert = getConditionOption(json.getString("QueryCondition["+index+"].conditionOption"), json.getString("QueryCondition["+index+"].conditionValue"));
			searchJson.put("conditionField", json.getString("QueryCondition["+index+"].conditionField"));
			searchJson.put("conditionOption", convert[0]);
			searchJson.put("conditionValue", convert[1]);
			searchJson.put("conditionValueLeft", json.getString("QueryCondition["+index+"].conditionValueLeft"));
			searchJson.put("conditionValueRight", json.getString("QueryCondition["+index+"].conditionValueRight"));
			searchJson.put("logicalOperator", json.getString("QueryCondition["+index+"].logicalOperator"));
			arr.add(searchJson);
		}
		return arr;
	}
	private static String [] getConditionOption(String option,String conditionValue){
		String result = option;
		String value = conditionValue;
		if(option.equals("equal")){
			result = "=";
			value = "'"+value+"'";
		}else if(option.equals("unequal")){
			result = "!=";
			value = "'"+value+"'";
		}else if(option.equals("like")){
			result = "like";
			value = "'%"+value+"%'";
		}else if(option.equals("empty")){
			result = "is null";
			value = "";
		}else if(option.equals("notempty")){
			result = "is not null";
			value = "";
		}else if(option.equals("greater_than")){
			result = ">";
		}else if(option.equals("less_than")){
			result = "<";
		}
		return new String[]{result,value};
	}
	/**
	 * 适用于将JSONArray中的唯一参数
	 * 
	 * @param lists
	 * @param field
	 * @return 返回拼接的字符串
	 */
	public static String getFieldByDouHao(JSONArray lists,String field){
		StringBuilder sb = new StringBuilder();
		for(int index=0;index<lists.size();index++){
			sb.append(lists.getJSONObject(index).getString(field)).append(",");
		}
		String result = "";
		if(lists.size()!=0)
			result =sb.toString().substring(0, sb.toString().lastIndexOf(","));
		return result;
	}
	/**
	 * 适用于将JSONArray中的唯一参数
	 * 
	 * @param lists
	 * @param field
	 * @return 返回拼接的带单引号的字符串
	 */
	public static String getFieldByDouHaoAndDanYinHao(JSONArray lists,String field){
		StringBuilder sb = new StringBuilder();
		for(int index=0;index<lists.size();index++){
			sb.append("'"+lists.getJSONObject(index).getString(field)+"'").append(",");
		}
		String result = "";
		if(lists.size()!=0)
			result =sb.toString().substring(0, sb.toString().lastIndexOf(","));
		return result;
	}
	/**
	 * 适用于将JSONArray中的唯一参数
	 * 
	 * @param lists
	 * @param field
	 * @return 返回拼接的字符串数组
	 */
	public static String[] getFieldByDouHaoArr(JSONArray lists,String field){
		String [] result = new String[lists.size()];
		for(int index=0;index<lists.size();index++){
			result[index] = lists.getJSONObject(index).getString(field);
		}
		return result;
	}
	/**
	 * 把新的参数字段名称值添加到旧的字符串中形成新的字符串
	 * @param newStr
	 * @return
	 */
	public static String addToStringArr(String newStr,String existStr){
		String [] exist = existStr.split(DefaultConfiguration.separator_douhao);
		boolean isExist = false;
		for(String str:exist){
			if(str.equals(newStr)){
				isExist = true;
				break;
			}
		}
		String result = "";
		if(isExist){
			result = existStr;
		}else{
			result = existStr+DefaultConfiguration.separator_douhao+newStr;
		}
		if(result.startsWith(DefaultConfiguration.separator_douhao)){
			result = result.substring(1);
		}
		return result;
	}
	public static String addToStringArr(String[] newStr,String existStr){
		for(String str:newStr){
			existStr = addToStringArr(str, existStr);
		}
		return existStr;
	}
	/**
	 * 把新的参数字段名称值从旧的字符串中移除形成新的字符串
	 * @param newStr
	 * @return
	 */
	public static String cutToStringArr(String newStr,String existStr){
		String [] exist = existStr.split(DefaultConfiguration.separator_douhao);
		int moveIndex = 0;
		for(int index=0;index<exist.length;index++){
			if(exist[index].equals(newStr)){
				moveIndex = index;
				break;
			}
		}
		String[] newStrArr = cut(exist,moveIndex);
		return StringUtils.join(newStrArr,DefaultConfiguration.separator_douhao);
	}
	public static String cutToStringArr(String [] newStr,String existStr){
		for(String str:newStr){
			existStr = cutToStringArr(str, existStr);
		}
		return existStr;
	}
	private static String[] cut(String[] arr,int index){
		String []newArr = new String[arr.length-1];
		for(int i=0;i<arr.length;i++){
			if(i<index)
				newArr[i] = arr[i];
			else if(i!=(arr.length-1))
				newArr[i] = arr[i+1];
		}
		return newArr;
	}
	/**
	 * 把json的value值相同的key值取出
	 * @param entity
	 * @param value
	 * @return
	 */
	public static String getJsonKeyByValue(JSONObject entity,String value){
		String result = "";
		for(String key:entity.keySet()){
			String getValue = entity.getString(key);
			if(StringUtils.equals(value, getValue)){
				result = key;
				break;
			}
		}
		return result;
	}
	public static String getSpecialStrBy(String conditionValue){
		String result = "";
		for(String key:conditionValue.split(",")){
			result += "?,";
		}
		return result.substring(0,result.lastIndexOf(","));
	}
	public static void main(String[] args) {
		String s = "a,b,c,d";
		System.out.println(cutToStringArr(new String[]{"a","c"}, s));
	}
	public static JSONObject convertTree(JSONObject obj,JSONObject TreeConvert){
		for(String key:TreeConvert.keySet()){
			obj.put(key, obj.getString(TreeConvert.getString(key)));
		}
		if(obj.getBoolean("haveChild"))
			obj.put("children",new JsonArray());
		return obj;
	}
}
